package com.steven.ourcontest.forum.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.steven.ourcontest.base.BaseViewModel
import com.steven.ourcontest.forum.api.ForumApi
import com.steven.ourcontest.forum.bean.ForumInfoList
import com.steven.ourcontest.utils.plusAssign
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class NewForumViewModel : BaseViewModel() {
    private val _newForumList = MutableLiveData<ForumInfoList?>()
    val newForumList: LiveData<ForumInfoList?>
        get() = _newForumList

    fun queryNewForumList(
        page: Long,
        onSuccess: (() -> Unit),
        onError: ((throwable: Throwable) -> Unit)
    ) {
        disposed += ForumApi.instance.queryNewForumList(page)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                _newForumList.value = it.result
                onSuccess.invoke()
            }, {
                it.printStackTrace()
                onError.invoke(it)
            })
    }
}